const express = require("express");
const util = require("../common/util");
const ReviewDB = require("../model/sqlite/review");

function addRiview(req, res) {
    (async () => {
        try {
            let review = {
                user_id: parseInt(req.body.user_id),
                goods_id: parseInt(req.body.goods_id),
                content: req.body.content
            };
            const db = ReviewDB.getInstance();
            await db.connect();
            util.err(`用户id：${typeof review.user_id}，内容：${review.content}`)
            let result = await db.add(review);

            if (result) {
                util.log(`评论成功，评论id：${result}`);
                return res.status(201).json(util.FormatJSONData(201, "评论成功", {
                    review_id: result
                }));
            }
        } catch {
            util.log("评论失败");
            return res.status(500).json(util.FormatJSONData(500, "服务器错误"));
        }
    })();
};

function usefulReview(req, res) {
    (async() => {
        try {
            let review_id = parseInt(req.query.review_id);
            let user_id = parseInt(req.query.user_id);
            const db = ReviewDB.getInstance();
            await db.connect();
            util.err(`${review_id},${user_id}`)
            let result = await db.useful(review_id,user_id);
            if (!result.changes){
                util.err(`点赞失败${result}`);
                return res.status(301).json(util.FormatJSONData(301,'请求失败'));
            } else{
                return res.status(201).json(util.FormatJSONData(201,'请求成功',{
                    reviewId: result.lastId
                }));
            }

        } catch (err) {
            util.err("点赞失败");
            return res.status(500).json(util.FormatJSONData(500, "服务器错误"));
        }
    })();
};

function uselessReview(req, res) {

};

function removeReview(req, res) {

};

function setPriority(req, res) {

};

function findById(req, res) {

};

function findByUser(req, res) {

};



module.exports = {
    addRiview,
    removeReview,
    usefulReview,
    uselessReview,
    findById,
    findByUser,
    removeReview,
    setPriority
};